#!/usr/bin/ruby
require 'find'

class Signer

	def gpg
		with_files do |path|
			cmd = "gpg -ab --passphrase #{ARGV[0]}  #{path}"
			puts cmd
			`#{cmd}`
		end
	end

	def md5
		with_files do |path|
			cmd = "md5 #{path}"
			result = `#{cmd}`
			result =~ /.+=\s(.+)/
			puts $1
			File.open("#{path}.md5", 'w') {|f| f.write($1)}
		end
	end
	
	def sha1 
		with_files do |path|
			cmd = "/usr/bin/openssl sha1 #{path}"
			result = `#{cmd}`
			result =~ /.+=\s(.+)/
			puts $1
			File.open("#{path}.sha1", 'w') {|f| f.write($1)}
		end
	end

	def with_files
		Find.find('../maven2/releases') do |path|
			filename = File.basename(path)
			next if !matched(File.extname(path))
			yield path
		end
	end

	def matched(filename)
		['.jar', '.pom', '.xml'].include? filename	
	end	

end

raise RuntimeError.new('passphrase required') if !ARGV[0]
signer = Signer.new
signer.gpg
signer.md5
signer.sha1
